<?php

class Outils_Date{
	
	
	public static function getFrDateFromSQL($sqldateString, $dayname = true,$subDayName = 0, $subMonthName = 0){	
		$date = strtotime($sqldateString);
		$j = date('d',$date);
		$jname = "";
		if ($dayname)
			$jname = self::getDayName(date('w',$date),$subDayName);
		$m = date('n',$date);
		$a = date('Y',$date);
		
		return $jname . " " . $j . " " . self::getMonthName($m,$subMonthName) . " " . $a;
	}
	
	public static function getMonthName($n,$sub=0){
		static $monthtab = array(
							1 => "Janvier",
							2 => "Février",
							3 => "Mars",
							4 => "Avril",
							5 => "Mai",
							6 => "Juin",
							7 => "Juillet",
							8 => "Aout",
							9 => "Septembre",
							10 => "Octobre",
							11 => "Novembre",
							12 => "Décembre",
						);
		if ($sub>0)				
			return substr($monthtab[$n],0,$sub);
		else
			return $monthtab[$n];	
	}
	
	public static function getDayName($n,$sub=0){
		static $daytab = array(
							0 => "Dimanche",
							1 => "Lundi",
							2 => "Mardi",
							3 => "Mercredi",
							4 => "Jeudi",
							5 => "Vendredi",
							6 => "Samedi"
						);
		if ($sub>0)				
			return substr($daytab[$n],0,$sub);
		else
			return $daytab[$n];
	}
	
	
	/*convertit la date ISO vers date PHP*/
	static public function isoTimeToStamp($iso) {
	  	sscanf($iso,"%4u-%u-%uT%u:%2u:%2uZ",$annee,$mois,$jour,$heure,$minute,$seconde);
	 	$newTstamp = mktime($heure,$minute,$seconde,$mois,$jour,$annee);
		return $newTstamp+3600;
	}
	
	/* mise en format francais */
	static public function tStampToFrancais($timestamp_chaine) {
		return date("d/m/y H:i:s", $timestamp_chaine);
	}
	
	static public function datefr($chaine) {
		return self::tStampToFrancais(self::isoTimeToStamp($chaine));
	}


/*
	Relative Time Function
	based on code from http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time/501415#501415
	For use in the "Parse Twitter Feeds" code below
*/
	static public function relativeTime($time){
		$second = 1;
		$minute = 60 * $second;
		$hour = 60 * $minute;
		$day = 24 * $hour;
		$month = 30 * $day;
		
		$delta = strtotime('+0 hours') - $time;
		if ($delta < 2 * $minute) {
			return "il y a 1 min";
		}
		if ($delta < 45 * $minute) {
			return "il y a ". floor($delta / $minute) . " min";
		}
		if ($delta < 90 * $minute) {
			return "il y a 1 heure";
		}
		if ($delta < 24 * $hour) {
			return "il y a ". floor($delta / $hour) . " heures";
		}
		if ($delta < 48 * $hour) {
			return "hier";
		}
		if ($delta < 30 * $day) {
			return "il y a ". floor($delta / $day) . " jours";
		}
		if ($delta < 12 * $month) {
			$months = floor($delta / $day / 30);
			return $months <= 1 ? "il y a un mois" : "il y a ". $months . " mois";
		} else {
			$years = floor($delta / $day / 365);
			return $years <= 1 ? "il y a un an" : "il y a ". $years . " ans";
		}
	}
	
	/* Secondes en HH:MM:SS    http://www.laughing-buddha.net/php/lib/sec2hms/ */
	
	public static function sec2hms ($sec, $padHours = false) {

		// start with a blank string
		$hms = "";
		
		// do the hours first: there are 3600 seconds in an hour, so if we divide
		// the total number of seconds by 3600 and throw away the remainder, we're
		// left with the number of hours in those seconds
		$hours = intval(intval($sec) / 3600); 
	
		// add hours to $hms (with a leading 0 if asked for)
		$hms .= ($padHours) 
			  ? str_pad($hours, 2, "0", STR_PAD_LEFT). ":"
			  : $hours. ":";
		
		// dividing the total seconds by 60 will give us the number of minutes
		// in total, but we're interested in *minutes past the hour* and to get
		// this, we have to divide by 60 again and then use the remainder
		$minutes = intval(($sec / 60) % 60); 
	
		// add minutes to $hms (with a leading 0 if needed)
		$hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ":";
	
		// seconds past the minute are found by dividing the total number of seconds
		// by 60 and using the remainder
		$seconds = intval($sec % 60); 
	
		// add seconds to $hms (with a leading 0 if needed)
		$hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
	
		// done!
		return $hms;
    
  }

	
	
}


?>